#!/bin/bash -e
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

usage() {
    echo "Usage: $0 <source-dir> <build-dir>" >&2
}

if [ $# -eq 2 ]; then
    SOURCE_DIR=$(realpath $1)
    BUILD_DIR=$(realpath $2)
elif [[ $# -eq 1 && ( "$1" = "-h" || "$1" = "--help" )]]; then
    usage
    exit 0
else
    echo "Wrong number of arguments: expected 2, was $#" >&2
    usage
    exit 1
fi

mkdir -p "${BUILD_DIR}"

source /opt/rh/devtoolset-6/enable || true
cd "${SOURCE_DIR}"
sh ./bootstrap.sh full
cd "${BUILD_DIR}"
cmake3 \
    -DCMAKE_INSTALL_PREFIX=/opt/vespa \
    -DJAVA_HOME=/usr/lib/jvm/java-openjdk \
    -DEXTRA_LINK_DIRECTORY="/opt/vespa-boost/lib;/opt/vespa-libtorrent/lib;/opt/vespa-zookeeper-c-client/lib;/opt/vespa-cppunit/lib;/usr/lib64/llvm3.9/lib" \
    -DEXTRA_INCLUDE_DIRECTORY="/opt/vespa-boost/include;/opt/vespa-libtorrent/include;/opt/vespa-zookeeper-c-client/include;/opt/vespa-cppunit/include;/usr/include/llvm3.9" \
    -DCMAKE_INSTALL_RPATH="/opt/vespa/lib64;/opt/vespa-boost/lib;/opt/vespa-libtorrent/lib;/opt/vespa-zookeeper-c-client/lib;/opt/vespa-cppunit/lib;/usr/lib/jvm/java-1.8.0/jre/lib/amd64/server;/usr/include/llvm3.9" \
    "${SOURCE_DIR}"
